import 'package:boss/models/commodity.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/back_confirmation.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'provider/plus_commodity_by_form_provider.dart';
import 'widget/plus_commodity_by_form_field_browser.dart';

@RouterConfig(PlusCommodityByFormFieldPage.pageName)
class PlusCommodityByFormFieldPage extends StatefulWidget {
  static const pageName = '/plusCommodityByFormFieldPage';

  @Autowired()
  final Commodity commodity;

  const PlusCommodityByFormFieldPage({this.commodity});

  @override
  _PlusCommodityByFormFieldPageState createState() => _PlusCommodityByFormFieldPageState('表单新增菜品');
}

class _PlusCommodityByFormFieldPageState extends BaseState<PlusCommodityByFormFieldPage> {
  _PlusCommodityByFormFieldPageState(String pageName) : super(pageName);

  @override
  Widget build(BuildContext context) {
    final commodityProvider = Provider.of<CommodityProvider>(
      context,
      listen: false,
    );

    return ChangeNotifierProvider(
      create: (ctx) => PlusCommodityByFormProvider(commodity: widget.commodity),
      child: BackConfirmation(
        backConfirmationText: '菜品还未完成确认返回么？',
        child: Scaffold(
          appBar: CenterTitleAppBar(
            title: '新增菜品',
            backConfirmationText: '菜品还未完成确认返回么？',
            actions: <Widget>[
              if (widget.commodity != null)
                LabelButton(
                  title: '删除',
                  padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                  onTap: () async {
                    var result = await commodityProvider.deleteCommodityByBean(widget.commodity);
                    if (result) {
                      Navigator.pop(context);
                    }
                  },
                ),
            ],
          ),
          body: PlusCommodityByFormFieldBrowser(),
        ),
      ),
    );
  }
}
